define(function(require, exports, module) {
  var Base = require('base');
  var $ = require('jquery');
  var LinkupModel = Base.Model.extend({
    defaults: {
      lineSpace: 20,
      columnSpace: 60,
      itemWidth: 200,
      itemHeight: 108
    },
    // 设置widget(连线项目/按钮/答案控件)的数据
    // 包括:
    //     连线项目所在列序号(从0开始)
    //     布局数据(top, left)
    setWidgetData: function(itemId, data) {
      var widgetsData = this.get("widgetsData");
      if (!widgetsData) widgetsData = {};
      if (widgetsData[itemId]) {
        $.extend(widgetsData[itemId], data);
      } else {
        widgetsData[itemId] = data;
      }
      widgetsData[itemId].itemId = itemId;
      this.set("widgetsData", widgetsData);
    },
    removeWidgetData: function(itemId) {
      var widgetsData = this.get("widgetsData");
      if (!widgetsData) return;
      if (!widgetsData[itemId]) return;
      delete widgetsData[itemId];
    },
    getWidgetDataById: function(itemId) {
      var widgetsData = this.get("widgetsData");
      return widgetsData[itemId];
    },
    getWidgetsDataByAttr: function(options) {
      var widgetsData = this.get("widgetsData");
      var data = [];
      var match;
      for (var i in widgetsData) {
        match = true;
        for (var key in options) {
          if (widgetsData[i][key] !== options[key]) {
            match = false;
            break;
          }
        }
        if (match) {
          data.push(widgetsData[i]);
        }
      }
      return data;
    },

    /**
      columns维护每一列的连线项目(按位置顺序)
      [
        [itemId, itemId, ...],
        [itemId, itemId, ...],
        ...
      ]
     */
    addItemToColumn: function(itemId, columnIndex) {
      var columns = this.get("columns");
      if (!columns) columns = [];
      if (columnIndex > columns.length) columnIndex = columns.length;
      if (columnIndex < 0) columnIndex = 0;
      if (!columns[columnIndex]) columns[columnIndex] = [];
      if (columns[columnIndex].indexOf(itemId) === -1) columns[columnIndex].push(itemId);
      this.set("columns", columns);
    },
    removeItemsFromColumn: function(num, columnIndex) {
      var columns = this.get("columns");
      if (!columns) return;
      if (columnIndex < 0 || columnIndex >= columns.length) return;
      if (num <= 0 || num > columns[columnIndex].length) return;
      if (num === columns[columnIndex].length) {
        columns.splice(columnIndex, 1);
        // 更新widgetsData中的列号
        var widgetsData = this.get("widgetsData");
        for (var i in widgetsData) {
          if (widgetsData[i].column > columnIndex) {
            widgetsData[i].column -= 1;
          }
        }
        this.set("widgetsData", widgetsData);
      } else {
        columns[columnIndex].splice(columns[columnIndex].length - num, num);
      }
      this.set("columns", columns);
    },
    removeItemFromColumn: function(itemId, columnIndex) {
      var columns = this.get("columns");
      var itemIndex;
      if (!columns) return;
      if (columnIndex < 0 || columnIndex >= columns.length) return;
      itemIndex = columns[columnIndex].indexOf(itemId);
      if (itemIndex === -1) return;
      columns[columnIndex].splice(itemIndex, 1);
      if (columns[columnIndex].length === 0) {
        columns.splice(columnIndex, 1);
        // 更新widgetsData中的列号
        var widgetsData = this.get("widgetsData");
        for (var i in widgetsData) {
          if (widgetsData[i].column > columnIndex) {
            widgetsData[i].column -= 1;
          }
        }
        this.set("widgetsData", widgetsData);
      }
      this.set("columns", columns);
    },
    moveItemInColumn: function(itemId, columnIndex, toIndex) {
      var columns = this.get("columns");
      var column = columns[columnIndex];
      if (!column) return;
      var fromIndex = column.indexOf(itemId);
      if (fromIndex === -1 || fromIndex === toIndex) return;
      column.splice(fromIndex, 1);
      column.splice(toIndex, 0, itemId);
      this.set("columns", columns);
    },
    insertColumn: function(columnIndex) {
      var columns = this.get("columns");
      columns.splice(columnIndex, 0, []);
      this.set("columns", columns);

      // 更新widgetsData中的列号
      var widgetsData = this.get("widgetsData");
      for (var i in widgetsData) {
        if (widgetsData[i].column >= columnIndex) {
          widgetsData[i].column += 1;
        }
      }
      this.set("widgetsData", widgetsData);
    },
    // 获取指定列的连线项目
    getItemIdsByColumn: function(columnIndex) {
      var columns = this.get("columns");
      if (!columns || !columns[columnIndex]) {
        return [];
      }
      return columns[columnIndex];
    },
  });
  return LinkupModel;
});